if (typeof ext == 'undefined') var ext = {};
ext.AppChanger = {

id: 'org.hotot.appchanger',

name: 'App Changer',

description: 'Change your source app. Create your own app at https://dev.twitter.com/apps',

version: '0.2',

author: 'Xu Zhen',

icon: "icon.png",

url: 'http://code.google.com/p/hotot-extensionz',

prefs: {
	"consumer_key": conf.vars.consumer_key,
	"consumer_secret": conf.vars.consumer_secret
},

def_prefs: {
	"consumer_key": conf.vars.consumer_key,
	"consumer_secret": conf.vars.consumer_secret
},

db: null,

open_option_dialog:
function open_option_dialog() {
	var title = 'App Setting';
	var body = '<style>'+
			'#ext_hotot_appchanger_opt_dialog table{width:100%}'+
			'#ext_hotot_appchanger_opt_dialog td{border:none;padding:0px}'+
			'#ext_hotot_appchanger_opt_dialog table input{width:250px}'+
		'</style>'+
		'<table>'+
		'<tbody>'+
		'<tr>'+
		'<td>Consumer Key:</td>'+
		'<td><input type="text" id="ext_hotot_appchanger_consumer_key"/></td>'+
		'</tr>'+
		'<tr>'+
		'<td>Consumer Secret:</td>'+
		'<td><input type="text" id="ext_hotot_appchanger_consumer_secret"/></td>'+
		'</tr>'+
		'</tbody>'+
		'</table>'+
		'<div>'+
		'<label>*Restart needed</label>'+
		'</div>';
		
	ext.AppChanger.option_dialog = widget.DialogManager.build_dialog(
		'#ext_hotot_appchanger_opt_dialog', title, '', body, [
			{id:'#ext_btn_appchanger_reset', label: 'Reset', click: ext.AppChanger.on_btn_reset_clicked},
			{id:'#ext_btn_appchanger_save', label: 'Save', click: ext.AppChanger.on_btn_save_prefs_clicked}
		]);
	ext.AppChanger.option_dialog.set_styles('header', {'display': 'none', 'height': '0'});
	ext.AppChanger.option_dialog.resize(400, 180);

	$("#ext_btn_appchanger_reset").css("padding-right", "10px");

	var prefs = ext.AppChanger.prefs;
	for (var key in prefs) {
		$('#ext_hotot_appchanger_' + key).val(prefs[key]);
	}
	
	ext.AppChanger.option_dialog.open();
},

on_btn_reset_clicked:
function on_btn_reset_clicked() {
	var prefs = ext.AppChanger.def_prefs;
	for (var key in prefs) {
		$('#ext_hotot_appchanger_' + key).val(prefs[key]);
	}
},

on_btn_save_prefs_clicked:
function on_btn_save_prefs_clicked() {

	var prefs = {
		"consumer_key": "",
		"consumer_secret": ""
	};
	for (var key in prefs) {
		var value = $('#ext_hotot_appchanger_' + key).val();
		if (value) {
			prefs[key] = value;
		} else {
			toast.set("Please fill the " + key.replace("_", " ") + " field").show();
			$('#ext_hotot_appchanger_' + key).focus();
			return;
		}
	}

	ext.AppChanger.prefs = prefs;
	ext.AppChanger.db.set('prefs', JSON.stringify(prefs));

	ext.AppChanger.option_dialog.close();
	ext.AppChanger.option_dialog = null;
},

enable:
function enable() {
	var key = ext.AppChanger.prefs.consumer_key;
	var secret = ext.AppChanger.prefs.consumer_secret;
	if (key && secret) {
		conf.vars.consumer_key = globals.twitterClient.oauth.key = key;
		conf.vars.consumer_secret = globals.twitterClient.oauth.secret = secret;
	}
},

disable:
function disable() {
},

options:
function options() {
	ext.AppChanger.open_option_dialog();
},

}

ext.AppChanger.db = new ext.Preferences(ext.AppChanger.id);
ext.AppChanger.db.get('prefs', function(key, val) {
	if (val) {
		var prefs = JSON.parse(val);
		if (prefs.consumer_key != null) {
			ext.AppChanger.prefs.consumer_key = prefs.consumer_key;
		}
		if (prefs.consumer_secret != null) {
			ext.AppChanger.prefs.consumer_secret = prefs.consumer_secret;
		}
		ext.AppChanger.enable();
	}
});
